<?php


class DbResult {

    /**
     * constructor
     * @param string $sql SQL query to execute
     */
    function DbResult($sql) {
        $this->sql=$sql;
    }

    /**
     * fetch a line as associative array
     * @access public
     * @return array
     */
    function fetchAssoc() {
        if (!$res=&$this->getResource())
            return false;
        return mysql_fetch_assoc($res);
    }

    /**
     * return lines count
     * @access public
     * @return int
     */
    function count() {
        if (!$res=&$this->getResource())
            return false;
        // if not buffered and lines remain, would be incorrect
        /*if (!$this->buffered and !$this->lines_remaining)
            return false;*/
        return mysql_num_rows($res);
    }

    /**
     * return resource. execute query if necessary
     */
    function & getResource() {
        if (!$this->resource) {
            $this->resource=mysql_unbuffered_query($this->sql);
            if (!$this->resource) {
                $this->error=mysql_error();
            }
        }
        return $this->resource;
    }

    /**
     * return sql error if occured
     */
    function getError() {
        return $this->error;
    }

    /**
     * @var resource
     */
    var $resource;

    /**
     * @var string sql query executed
     */
    var $sql;

    /**
     * @var string SQL error if occured
     */
    var $error;

    /**
     * @var bool if result is buffered
     */
    var $buffered=false;
}
